package com.small.nacos.core.cluster;

import com.alibaba.nacos.api.exception.NacosException;
import com.small.nacos.core.cluster.Member;
import com.small.nacos.core.cluster.MemberLookup;
import com.small.nacos.core.cluster.ServerMemberManager;

import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Author zhoujin
 * @Date 2022/11/9 14:08
 */
public abstract class AbstractMemberLookup implements MemberLookup {

    protected AtomicBoolean start = new AtomicBoolean(false);

    protected ServerMemberManager memberManager;


    @Override
    public void start() throws NacosException {
        if (start.compareAndSet(false,true)) {
            doStart();
        }
    }

    @Override
    public void injectMemberManager(ServerMemberManager memberManager) {
        this.memberManager=memberManager;
    }

    @Override
    public void afterLookup(Collection<Member> members) {
        this.memberManager.memberChange(members);
    }

    @Override
    public void destroy() throws NacosException {

    }

    protected abstract void doStart();
}
